package com.powernode.config;

import com.powernode.constant.Constants;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.CustomExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

/**
 * 延迟队列 超时未支付 消费者配置类
 * 配置自定义交换机
 */
@Configuration
public class DelayRabbitConfig {

    // 定义延迟交换机
    @Bean
    public CustomExchange delayCustomExchange() {
        HashMap<String, Object> arguments = new HashMap<>();
        arguments.put("x-delayed-type", "direct");
        return new CustomExchange(Constants.EXCHANGE_DELAY_NAME, "x-delayed-message", true, false , arguments);
    }

    // 定义队列
    @Bean
    public Queue delayCustomQueue() {
        return new Queue(Constants.QUEUE_DELAY_NAME);
    }

    // 定义绑定
    @Bean
    public Binding bindingDelayQueuetoCustomEx() {
        return BindingBuilder.bind(delayCustomQueue()).to(delayCustomExchange()).with(Constants.ROUTING_KEY_DELAY_NAME).noargs();
    }
}
